import { FileTypeList } from '../Config/typelist'
import { parseURLParams } from './Common'

// TODO 判断浏览器版本
export const judgeBrowserVersion = () => {
    const browserVersions = { Chrome: 93, Safari: 530, Firefox: 92, Opera: 80 }
    const { userAgent } = window.navigator
    const webList = userAgent.split(' ').filter(v => v.indexOf('/') !== -1 && v.indexOf('Mozilla') === -1 && v.indexOf('AppleWebKit') === -1)
    let currentVersions = {}
    for (const key of webList) {
        const [keyName, value] = key.split('/')
        currentVersions[keyName] = parseInt(value)
    }
    return Object.keys(browserVersions).every(key => {
        if (currentVersions[key] && browserVersions[key]) { return currentVersions[key] >= browserVersions[key] }
        return true
    })
}

// TODO Debug函数
export const debuggerFun = () => {
    const block = () => {
        if (window.outerHeight - window.innerHeight > 200 || window.outerWidth - window.innerWidth > 200) {
            document.body.innerHTML = "检测到非法调试,请关闭后刷新重试!";
            sessionStorage.clear()
            localStorage.clear()
        }
        setInterval(() => {
            (function () { return false }
            // eslint-disable-next-line no-unexpected-multiline
            ['constructor']('debugger')
            // eslint-disable-next-line no-unexpected-multiline
            ['call']())
        }, 50)
    }
    try { block() } catch (err) { /* empty */ }
}

// TODO 判断单点登录
export const singleSignOn = (search) => {
    const { token, loginType } = parseURLParams(search)
    console.log(token, loginType)
}

// TODO 判断字段所在对象数组的Key值
export const judegStringOfArrKey = (obj, uuid) => {
    return Object.entries(obj).find(([, value]) => Array.isArray(value) && value.includes(uuid))?.shift()
}

// TODO 判断文件类型
export const getFileType = (filename) => {
    const extension = filename.substring(filename.lastIndexOf('.'))
    return FileTypeList[extension] || 'unknown'
}

// TODO 大屏判断首页
export const judegFrontHome = (url) => {
    if (url === '/Front') return true
    else return false
}